var oldmark;
function getCountries(userCountry, userProvince, userCity) {
	doAjax(SKYLINE_DOMAIN + '/site/getCountries.html', '', function(data) {
		if (data && data.length > 0) {
			var countrySelect = $('#country')[0];
			countrySelect.options.length = 1;
			for ( var i = 0; i < data.length; i++) {
				var option = new Option(data[i].name, data[i].name + "-"
						+ data[i].id);
				if (userCountry && data[i].name == userCountry) {
					option.selected = true;
				}
				countrySelect.options.add(option);
			}
			if ($('#country').val() != "") {
				getProvinces(userProvince, userCity);
			}
		} else {
			getProvinces("", "");
			return false;
		}
	});
}
function getProvinces(userProvince, userCity) {
	var country = $('#country').val();
	var provinceSelect = $('#province')[0];
	provinceSelect.options.length = 1;
	if (country == "") {
		getCities("");
		return false;
	}
	var countryId = country.split("-")[1];
	doAjax(SKYLINE_DOMAIN + '/site/getSubSites.html', 'siteId=' + countryId,
			function(data) {
				if (data && data.length > 0) {
					for ( var i = 0; i < data.length; i++) {
						var option = new Option(data[i].name, data[i].name
								+ "-" + data[i].id);
						if (userProvince && data[i].name == userProvince) {
							option.selected = true;
						}
						provinceSelect.options.add(option);
					}
					if ($('#province').val() != "") {
						getCities(userCity);
					}
				} else {
					getCities("");
					return false;
				}
			});
}
function getCities(userCity) {
	var province = $('#province').val();
	var citySelect = $('#city')[0];
	citySelect.options.length = 1;
	if (province == "") {
		return false;
	}
	var provinceId = province.split("-")[1];
	doAjax(SKYLINE_DOMAIN + '/site/getSubSites.html', 'siteId=' + provinceId,
			function(data) {
				if (data && data.length > 0) {
					for ( var i = 0; i < data.length; i++) {
						var option = new Option(data[i].name, data[i].name
								+ "-" + data[i].id);
						if (userCity && data[i].name == userCity) {
							option.selected = true;
						}
						citySelect.options.add(option);
					}
				} else {
					return false;
				}
			});
}
function checkAttractionExist(name) {
	doAjax(SKYLINE_DOMAIN + "/attraction/isExist.html", "name=" + name,
			function(exist) {
				nameChecked = true;
				if (exist) {
					$("#nameExist").show();
				} else {
					$("#nameExist").hide();
				}
				nameExist = exist;
			});
}
(function($) {
	$(document).ready(function() {
		initMap("mapContainer");
		map.addEventListener("click", function(e) {
			$("#longitude").val(e.point.lng);
			$("#latitude").val(e.point.lat);
			processAddress(e.point,'city','detailAddress');
			map.removeOverlay(oldmark);
			var marker = createDraggableMarker(e.point, function(evnt){
				$("#longitude").val(evnt.point.lng);
				$("#latitude").val(evnt.point.lat);
				processAddress(evnt.point,'city','detailAddress');
				popAddressInfo(marker, evnt.point, 'name');
			});
			oldmark = marker;
			addMarkerOnMap(map, marker);
			popAddressInfo(marker, e.point, 'name');
		});
		$("#suggest").click(function(){
			var cityVal = $("#city").val();
			if(cityVal == '') {
				alert("请先选择要搜索的城市");
				$("#city").focus();
				return false;
			}
			this.select();
			var city = cityVal.split('-')[0];
			var ac;
			var callback = function(location){
				if(location.city == city) {
					$("#serchLocation").click();
					return false;
				}
				if(jsSelectItemByValue($("#city")[0], location.city)) {
					city = location.city;
					ac.setLocation(location.city);
					$("#serchLocation").click();
					return false;
				}
				if(jsSelectItemByValue($("#city")[0], location.district)) {
					city = location.district;
					ac.setLocation(location.district);
					$("#serchLocation").click();
					return false;
				}
				$('#searchMsg').html('我们无法自动搜索到您选择的地点，可能该地点不在该省内，您可以重新选择省份。或者直接点击搜搜看');
				$('#searchMsg').show('fast').delay(5000).hide('fast');
				$("#province").focus();
			};
			ac = createAutocomplete(this, map, callback);
		});
		$("#serchLocation").click(function(){
			var cityVal = $("#city").val();
			if(cityVal == '') {
				alert("请先选择要搜索的城市");
				$("#city").focus();
				return false;
			}
			var city = cityVal.split('-')[0];
			var suggest = $("#suggest").val();
			serchFromCityByName(city,suggest,null,function(point){
				processAddress(point,'city','detailAddress');
			});
			return false;
		});
	});
})(jQuery);